#include "CTexture.h"


CTexture::CTexture()
{
	m_pTexture			= NULL;
	m_pSpriteHandler	= NULL;
}

CTexture::~CTexture()
{
	Release();
}

//-------------------------------------------------//
// Name : Release                                  //
// Desc : Release the object , free memory etc.... //
//-------------------------------------------------//
void CTexture::Release()
{
	if(m_pTexture)
		m_pTexture->Release();
	if(m_pSpriteHandler)
		m_pSpriteHandler->Release();

	m_pTexture			= NULL;
	m_pSpriteHandler	= NULL;
}


//-----------------------------------------------//
// Name : LoadTextureFromFile                    //
// Desc : Use to load an texture image from file //
//-----------------------------------------------//
HRESULT	CTexture::LoadTextureFromFile(LPCTSTR FilePath, D3DCOLOR TransparentColor, LPDIRECT3DDEVICE9 pD3dDevice , LPD3DXSPRITE spriteHandler)
{
	HRESULT					hr;
	D3DXIMAGE_INFO			imageInfo;


	if(!spriteHandler || !pD3dDevice)
		return D3DERR_INVALIDCALL;

	// store the LPD3DXSPRITE pointer
	m_pSpriteHandler = spriteHandler;
	//Increase ref value so remember to release !!!
	m_pSpriteHandler->AddRef();

	hr = D3DXGetImageInfoFromFile( FilePath , &imageInfo );

	if(FAILED(hr))
		return hr;

	
	hr = D3DXCreateTextureFromFileEx( pD3dDevice ,				// DirectX device
										FilePath ,				// File Path
										imageInfo.Width ,		// width
										imageInfo.Height ,		// Height
										imageInfo.MipLevels ,	// MipLevels
										D3DUSAGE_DYNAMIC ,		// D3DUSAGE
										imageInfo.Format ,		// Format
										D3DPOOL_DEFAULT ,		// Pool
										D3DX_DEFAULT ,			// Filter
										D3DX_DEFAULT ,			// Mip filter
										TransparentColor ,		// Color key
										&imageInfo ,			// Image Info
										NULL ,					// NULL
										&m_pTexture );			// Output texture

	if(FAILED(hr))
		return hr;

	return D3D_OK;
}


//------------------------------//
// Name : Draw                  //
// Desc : Draw a normal texture //
//------------------------------//
void CTexture::Draw(float Xpos, float Ypos, float deep, 
					const RECT *scrRect , D3DCOLOR color)
{
	/*D3DXMATRIX		mtxTransform;
	D3DXMatrixIdentity(&mtxTransform);
	m_pSpriteHandler->SetTransform(&mtxTransform);*/

	m_pSpriteHandler->Draw( m_pTexture , scrRect , NULL , &D3DXVECTOR3( Xpos , Ypos , deep ) , color );
}


//-----------------------------------------------------------//
// Name : Draw												 //
// Desc : Draw a texture that can scale , rotate , translate //
//-----------------------------------------------------------//
void CTexture::Draw( float x, float y, 
					D3DXVECTOR2 vecScaleCenter , float xScaleRatio, float yScaleRatio, 
					D3DXVECTOR2 vecRotatteCenter, float angle, 
					D3DCOLOR color, const RECT *scrRect, float deep)
{
	D3DXMATRIX		mtxTransform;

	D3DXMatrixTransformation2D( &mtxTransform , &vecScaleCenter , 0 , 
								&D3DXVECTOR2(xScaleRatio , yScaleRatio ) , 
								&vecRotatteCenter , D3DXToRadian(angle) , 
								&D3DXVECTOR2( x , y ) );

	m_pSpriteHandler->SetTransform(&mtxTransform);

	m_pSpriteHandler->Draw( m_pTexture , scrRect , NULL , &D3DXVECTOR3( 0 , 0 , deep ) , color );
}


//---------------------------------//
// Name : Draw                     //
// Desc : Draw an animation sprite //
//---------------------------------//
void CTexture::Draw( int x, int y, 
					float xScaleRatio, float yScaleRatio, 
					char currFrame , char NumFrameInCol, float deep, int width, int height )
{
	RECT		scrRect;

	scrRect.left	=	( currFrame % NumFrameInCol ) * width;
	scrRect.top		=	( currFrame / NumFrameInCol ) * height;
	scrRect.right	=	scrRect.left + width;
	scrRect.bottom	=	scrRect.top + height;

	D3DXMATRIX		mtxTransform;

	D3DXMatrixTransformation2D( &mtxTransform , NULL , 0 , 
								&D3DXVECTOR2(xScaleRatio , yScaleRatio ) , 
								NULL , 0 , 
								&D3DXVECTOR2( x , y ) );

	m_pSpriteHandler->SetTransform(&mtxTransform);

	m_pSpriteHandler->Draw( m_pTexture , &scrRect , NULL , &D3DXVECTOR3( 0 , 0 , deep ) , D3DCOLOR_XRGB(255,255,255) );
}